package com.yiyuan.weixin.weixinInservice;

import com.yiyuan.weixin.utils.WechatProcess;
import com.yiyuan.weixin.weixinInservice.Iml.WXAccessFirstIml;
import com.yiyuan.weixin.weixinInservice.Iml.WXAccessIml;
import com.yiyuan.weixin.weixinInservice.Iml.WeiXinAccessParentIml;
import com.yiyuan.weixin.weixinInservice.service.WXAccessService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by admin on 2017/3/22.
 * 用户、微信请求   入口
 */
public  class WeiXinAccessServlet extends HttpServlet {
    public static final double DOUBLE = 40.;
//    WXAccessService weiXinAccess=new WXAccessFirstIml();
    WXAccessService weiXinAccess=new WXAccessIml();
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            weiXinAccess.weiXinAccess(req,resp);
    }


//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//            request.setCharacterEncoding("UTF-8");
//            response.setCharacterEncoding("UTF-8");
//
//            /** 读取接收到的xml消息 */
//            StringBuffer sb = new StringBuffer();
//            InputStream is = request.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
//            BufferedReader br = new BufferedReader(isr);
//            String s = "";
//            while ((s = br.readLine()) != null) {
//                sb.append(s);
//            }
//            String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据
//        System.out.println("解析之前："+xml);
//
//            String result = "";
//            /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */
//            String echostr = request.getParameter("echostr");
//            if (echostr != null && echostr.length() > 1) {
//                result = echostr;
//            } else {
//                //正常的微信处理流程
//                result = new WechatProcess().processWechatMag(xml);
//            }
//
//            try {
//                OutputStream os = response.getOutputStream();
//                os.write(result.getBytes("UTF-8"));
//                os.flush();
//                os.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        System.out.println("格式化后："+result);
//        }
//
//
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doGet(req,resp);
//    }

}
